﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Facebook;

namespace FBEventMaker.Facebook
{
    public partial class FacebookEventPage : PhoneApplicationPage
    {
        private string m_AccessToken;
        public FacebookEventPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            m_AccessToken = NavigationContext.QueryString["access_token"];

            GpsServices gpsServiceForLocation = new GpsServices();
            gpsServiceForLocation.GpsServiceCompleted += (gpsLocObj, gpsLocEvent) =>
                {
                    m_Location.Text = gpsLocEvent.Address;
                };
            gpsServiceForLocation.GetCurrentLocationAddress();
        }

        
        string m_LastEventId;
        private void m_CreateEventButton_Click(object sender, RoutedEventArgs e)
        {
            string errorMessage = null;
            if (string.IsNullOrEmpty(m_Name.Text.Trim()))
            {
                errorMessage = "Enter Event Name";
            }

            if (errorMessage != null)
            {
                MessageBox.Show(errorMessage);
                return;
            }

            FacebookClient fb = new FacebookClient(m_AccessToken);

            #region service routine            
            fb.PostCompleted += (o, args) =>
            {              
                if (args.Cancelled)
                {                    
                    var cancellationError = args.Error;
                }
                else if (args.Error != null)
                {
                    // error occurred
                    Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show(args.Error.Message);
                    });
                }
                else
                {                   
                    var result = (IDictionary<string, object>)args.GetResultData();
                    m_LastEventId = (string)result["id"];                   
                    Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show("Event created successfully");
                        m_Name.Text = string.Empty;                        
                    });
                }
            };
            #endregion

            var parameters = new Dictionary<string, object>();
            parameters["name"] = m_Name.Text;
            parameters["start_time"] = m_StartTime.ValueString;
            parameters["end_time"] = m_EndTime.ValueString;
            parameters["location"] = m_Location.Text;           
            parameters["description"] = m_Description.Text;
            try
            {
                fb.PostAsync("me/events", parameters);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }       
        }
    }
}